package com.cjfather.redis.steam.consumers;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @author: todd
 * @date:   2023-08-08
 */

@Slf4j
@Component
@Data
//@Builder
@Scope("prototype")
public class UserStreamConsumer implements ConsumerI{
	/**
     * 消费组
     */
    private String group;
    /**
     * 消费组中的某个消费者
     */
    private String consumerName;
    
    @Autowired
    private StringRedisTemplate redisTemplate;
    
    @Override
    public void onMessage(MapRecord<String, Object, Object> message) {
    	String key = message.getStream();
        // 消息ID
        RecordId messageId = message.getId();
        // 消息的key和value
        Map<Object, Object> body = message.getValue();
        log.info("key:{},组:{},消费者{} + stream message。messageId={}, body={}",key, group,consumerName,messageId, body);
        
        redisTemplate.opsForStream().delete(key, messageId);
    }
}
